<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//echo $this->config->base_url();
require_once($_SERVER['DOCUMENT_ROOT']."/application/3rdparty/PHPExcel/Classes/PHPExcel.php");
class PHPExport {
	
	private static $m_pInstance; 
    private function __construct() {} 
	private $data;
	private $method;
	private $template;
	private $format;
	
    
	public static function getInstance() 
    { 
        if (!self::$m_pInstance) 
        { 
            self::$m_pInstance = new RequestManager(); 
        } 
        return self::$m_pInstance; 
    } 
    
    public function setData($data){
    	$this->data = $data;
    }
    
	public function setTemplate($template){
    	$this->template = $template;
    }
    
	public function setFormat($format){
    	$this->format = $format;
    }
    
    public function send(){
    	switch($this->template) {
    		case 'payroll_journal':
    			_payroll_journal();
    			break;
    		
    	}
    }
    
    private function _payroll_journal(){
    	$objPHPExcel = new PHPExcel();
    	$data = $this->data;
    	$coverage = $data['period']['from'] .' - '. $data['period']['to'];
		$payroll_coverage = "Payroll for the Period covered: {$coverage}";
		$objPHPExcel->getProperties()->setCreator("{$data['info']['creator_name']}")
									 ->setLastModifiedBy("{$data['info']['current_user']}")
									 ->setTitle("{$data['info']['title']}")
									 ->setSubject("{$data['info']['subject']}")
									 ->setDescription("{$data['info']['description']}")
									 ->setKeywords("{$data['info']['keywords']}")
									 ->setCategory("{$data['info']['category']}");
		
		
		// Add some data
		$row = 4;
		
		$objPHPExcel->setActiveSheetIndex(0);
		
		$data_label = $data['label'];
		/*$data['label'] = array('Regular Hours',
		'Regular Overtime', 'Reg. Night Diff',
		'Restday','Excess on Restday','Restday Night Diff',
		'Special Holiday','Excess on Special Holiday','Special Holiday Night Diff',
		'Special Holiday on Restday','Excess Special Holiday on Restday',
		'Legal Holiday','Excess on Legal Holiday','Legal Holiday Unwork',
		'Legal Holiday on Restday','Excess Legal Holiday on Restday',
		'Legal Holiday Night Diff','Adjustment/CA','Incentives/Maternity/Sickness Reimbursement');*/
		
		$data_label = array('Regular Hours',
		'Regular Overtime', 'Reg. Night Diff',
		'Restday');
		
		
		$user_rates = array('regular_hours'=>38.13,
		'regular_overtime'=>47.66,
		'regular_night_diff'=>3.81,
		'rest_day'=>49.56);
		
		$user_hours = array('regular_hours'=>120,
		'regular_overtime'=>5,
		'regular_night_diff'=>10,
		'rest_day'=>16);
		
		$users[] = array('name'=>'Jomel Vicente','user_hours'=>$user_hours, 'user_rates'=>$user_rates);
		$users[] = array('name'=>'Elimarie Vicente','user_hours'=>$user_hours, 'user_rates'=>$user_rates);
		
		//var_dump($users);
		//die();
		
		
		$row = 4;
		$header_base_row = 3;
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$header_base_row, $payroll_coverage);
		$objPHPExcel->getActiveSheet()->getStyle('A'.$header_base_row)->getFont()->setSize(10);
		$objPHPExcel->getActiveSheet()->setCellValue('H'.$header_base_row, $payroll_coverage);
		$objPHPExcel->getActiveSheet()->getStyle('H'.$header_base_row)->getFont()->setSize(10);
		
		$objDrawing = new PHPExcel_Worksheet_Drawing();
		$objDrawing->setName('Logo');
		$objDrawing->setDescription('Logo');
		$objDrawing->setPath('/images/time_republic_logo.jpg');
		$objDrawing->setHeight(27);
		$objDrawing->setCoordinates('A1');
		$objDrawing->setOffsetX(5);
		$objDrawing->setOffsetY(5);
		$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
		
		$objDrawing2 = new PHPExcel_Worksheet_Drawing();
		$objDrawing2->setName('Logo');
		$objDrawing2->setDescription('Logo');
		$objDrawing2->setPath('/images/time_republic_logo.jpg');
		$objDrawing2->setHeight(27);
		$objDrawing2->setCoordinates('H1');
		$objDrawing2->setOffsetX(5);
		$objDrawing2->setOffsetY(5);
		$objDrawing2->setWorksheet($objPHPExcel->getActiveSheet());
		
		$user_count = 0;
		$rowBase = 4;
		foreach($users as $user){
			$user_count++;
			if($user_count % 2 == 0) {
				$row = $rowBase;
				for($i=0; $i < count($data_label); $i++){
					$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $data_label[$i]);
					$objPHPExcel->getActiveSheet()->getStyle('H'.$row)->getFont()->setSize(10);
					$row++;
				}
				
				$row = $rowBase;
				foreach($user['user_hours'] as $value){
					$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, $value);
					$objPHPExcel->getActiveSheet()->getStyle('J'.$row)->getFont()->setSize(10);
					$row++;
				}
				
				$row = $rowBase;
				foreach($user['user_rates'] as $value){
					$objPHPExcel->getActiveSheet()->setCellValue('K'.$row, $value);
					$objPHPExcel->getActiveSheet()->getStyle('K'.$row)->getFont()->setSize(10);
					$row++;
				}
				$row = $rowBase;
				for($i=0; $i < count($user_rates ); $i++){
					$valueStr = "=J{$row}*K{$row}";
					$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, "{$valueStr}");
					$objPHPExcel->getActiveSheet()->getStyle('L'.$row)->getFont()->setSize(10);
					$row++;
				}
				
			} else {
				$row = $rowBase;
				for($i=0; $i < count($data_label); $i++){
					$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $data_label[$i]);
					$objPHPExcel->getActiveSheet()->getStyle('A'.$row)->getFont()->setSize(10);
					$row++;
				}
				$row = $rowBase;
				foreach($user['user_hours'] as $value){
					$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $value);
					$objPHPExcel->getActiveSheet()->getStyle('C'.$row)->getFont()->setSize(10);
					$row++;
				}
				
				$row = $rowBase;
				foreach($user['user_rates'] as $value){
					$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $value);
					$objPHPExcel->getActiveSheet()->getStyle('D'.$row)->getFont()->setSize(10);
					$row++;
				}
				
				$row = $rowBase;
				for($i=0; $i < count($user_rates ); $i++){
					$valueStr = "=C{$row}*D{$row}";
					$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, "{$valueStr}");
					$objPHPExcel->getActiveSheet()->getStyle('E'.$row)->getFont()->setSize(10);
					$row++;
				}
				
				
				
			}
			
		}
		/*foreach($fields_array as $key=>$value){
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $value);
			$objPHPExcel->getActiveSheet()->getStyle('A'.$row)->getFont()->setSize(10);
			$row++;
		}*/
		
		$styleThinBlackBorderOutline = array(
			'borders' => array(
				'outline' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
					'color' => array('argb' => 'FF000000'),
				),
			),
		);
		$objPHPExcel->getActiveSheet()->getStyle('A1:F10')->applyFromArray($styleThinBlackBorderOutline);
					
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('Simple');
		
		
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
		
		
		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="payroll_' . date('Y-m-d').'.xls"');
		header('Cache-Control: max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
    	
    }

}